package com.t.bricks.bussiness.controller.encode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.encode.AESUtil;

@RestController
@Api(value="系统秘钥加密", tags={"系统秘钥加密操作接口"})
@RequestMapping("/system/key/")
public class SystemKeyController {

	/**
	 * 创建系统秘钥
	 * @param cpuid 硬件编号
	 * @param content 待加密值
	 * @param password 密钥
	 * @return
	 */
	//[start]
	@ApiOperation(value="创建系统秘钥")
	@ApiImplicitParams({
		@ApiImplicitParam(name="cpuid", value="硬件编号", dataType="String", paramType="query", required=true),
		@ApiImplicitParam(name="content", value="待加密值", dataType="String", paramType="query", required=true),
		@ApiImplicitParam(name="password", value="待加密值", dataType="String", paramType="query", required=true)
	})
	@ApiResponses({
		@ApiResponse(code=200, message="{\"success\": 布尔值, \"msg\": 文字提示, \"data\": 码值}"),
		@ApiResponse(code=400, message="错误的请求,请检查路径"),
		@ApiResponse(code=404, message="请求路径没有或页面跳转路径不对"),
		@ApiResponse(code=1000001, message="内部请求密钥错误"),
		@ApiResponse(code=1000002, message="您还没有登录或登录已超时,需要重新登录"),
		@ApiResponse(code=1000003, message="检查登录状态失败,请稍后重试！"),
		@ApiResponse(code=1000004, message="请求的操作不存在,请检查访问地址正确性，如大小写"),
		@ApiResponse(code=1000005, message="响应后在系统内部发生不可预知的错误"),
		@ApiResponse(code=1019000, message="1019000~1999999为拦截器调用方法错误"),
		@ApiResponse(code=9001, message="加密失败")
	})
	//[end]
	@RequestMapping("/encode")
	public MsgEmity encode(String cpuid, String content, String password) {
		StringBuilder sb = new StringBuilder(
				cpuid.length() + content.length() + password.length() + 2
			);
		sb.append(cpuid);
		sb.append(";");
		sb.append(content);
		
		String data = AESUtil.encode(sb.toString(), password);
		
		if (null == data || "".equals(data)) {
			return MsgEmity.err(9001, "加密失败");
		}
		
		return new MsgEmity(true, "加密成功", data);
	}

	/**
	 * 对系统秘钥解密
	 * @param content 待加密值
	 * @param password 密钥
	 * @return
	 */
	//[start]
	@ApiOperation(value="对系统秘钥解密")
	@ApiImplicitParams({
		@ApiImplicitParam(name="date", value="待加密值", dataType="String", paramType="query", required=true),
		@ApiImplicitParam(name="password", value="待加密值", dataType="String", paramType="query", required=true)
	})
	@ApiResponses({
		@ApiResponse(code=200, message="{\"success\": 布尔值, \"msg\": 文字提示, \"data\": 码值}"),
		@ApiResponse(code=400, message="错误的请求,请检查路径"),
		@ApiResponse(code=404, message="请求路径没有或页面跳转路径不对"),
		@ApiResponse(code=1000001, message="内部请求密钥错误"),
		@ApiResponse(code=1000002, message="您还没有登录或登录已超时,需要重新登录"),
		@ApiResponse(code=1000003, message="检查登录状态失败,请稍后重试！"),
		@ApiResponse(code=1000004, message="请求的操作不存在,请检查访问地址正确性，如大小写"),
		@ApiResponse(code=1000005, message="响应后在系统内部发生不可预知的错误"),
		@ApiResponse(code=1019000, message="1019000~1999999为拦截器调用方法错误"),
		@ApiResponse(code=9001, message="解密失败")
	})
	//[end]
	@RequestMapping("/decode")
	public MsgEmity decode(String content, String password) {
		String data = AESUtil.decode(content, password);
		
		if (null == data || "".equals(data)) {
			return MsgEmity.err(9001, "解密失败");
		}
		
		return new MsgEmity(true, "解密成功", data);
	}
	
}
